package com.rt.easyjava.common.utils.zip;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.util.Enumeration;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;

@Slf4j
public class ZipUtils {

    /**
     * 压缩文件路径保持原文件中的路径
     * @param srcfile
     * @param out
     */
    public static void zipFilesContainPath(File[] srcfile,  ZipOutputStream out){
        byte[] buf=new byte[1024];
        try {
            for(int i=0;i<srcfile.length;i++){
                File file = (File)srcfile[i];
                if (!file.exists()) {
                    log.info("file not exist: {}", file.getPath());
                    continue;
                }
                if (file.isFile()) {
                    FileInputStream in = new FileInputStream(file);
                    out.putNextEntry(new ZipEntry(file.getPath()));
                    int len;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                    out.closeEntry();
                    in.close();
                }else {
                    File[] listFiles = file.listFiles();
                    for (File file1 : listFiles) {
                        FileInputStream in = new FileInputStream(file1);
                        out.putNextEntry(new ZipEntry(file.getPath()));
                        int len;
                        while ((len = in.read(buf)) > 0) {
                            out.write(buf, 0, len);
                        }
                        out.closeEntry();
                        in.close();
                    }
                }
            }
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 功能:压缩多个文件成一个zip文件
     * @param srcfileMap：源文件列表
     * @param zipfile：压缩后的文件
     */
    public static void zipFilesContainPath(Map[] srcfileMap, File zipfile){
        byte[] buf=new byte[1024];
        try {
            ZipOutputStream out=new ZipOutputStream(new FileOutputStream(zipfile));
            for(int i=0;i<srcfileMap.length;i++){
                String path = (String)srcfileMap[i].get("path");
                File file = (File)srcfileMap[i].get("file");
                if (!file.exists()) {
                    log.info("file not exist: {}", file.getPath());
                    continue;
                }
                if (file.isFile()) {
                    FileInputStream in = new FileInputStream(file);
                    out.putNextEntry(new ZipEntry(path + File.separator + file.getName()));
                    int len;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                    out.closeEntry();
                    in.close();
                }else {
                    File[] listFiles = file.listFiles();
                    for (File file1 : listFiles) {
                        FileInputStream in = new FileInputStream(file1);
                        out.putNextEntry(new ZipEntry(path + File.separator + file1.getName()));
                        int len;
                        while ((len = in.read(buf)) > 0) {
                            out.write(buf, 0, len);
                        }
                        out.closeEntry();
                        in.close();
                    }
                }
            }
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void zipFilesContainPath(Map[] srcfileMap,  ZipOutputStream out){
        byte[] buf=new byte[1024];
        try {
            for(int i=0;i<srcfileMap.length;i++){
                String path = (String)srcfileMap[i].get("path");
                File file = (File)srcfileMap[i].get("file");
                if (!file.exists()) {
                    log.info("file not exist: {}", file.getPath());
                    continue;
                }
                if (file.isFile()) {
                    FileInputStream in = new FileInputStream(file);
                    out.putNextEntry(new ZipEntry(path + File.separator + file.getName()));
                    int len;
                    while ((len = in.read(buf)) > 0) {
                        out.write(buf, 0, len);
                    }
                    out.closeEntry();
                    in.close();
                }else {
                    File[] listFiles = file.listFiles();
                    for (File file1 : listFiles) {
                        FileInputStream in = new FileInputStream(file1);
                        out.putNextEntry(new ZipEntry(path + File.separator + file1.getName()));
                        int len;
                        while ((len = in.read(buf)) > 0) {
                            out.write(buf, 0, len);
                        }
                        out.closeEntry();
                        in.close();
                    }
                }
            }
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 功能:压缩多个文件成一个zip文件
     * @param srcfile：源文件列表
     * @param zipfile：压缩后的文件
     */
    public static void zipFiles(File[] srcfile, File zipfile){
        byte[] buf=new byte[1024];
        try {
            //ZipOutputStream类：完成文件或文件夹的压缩
            ZipOutputStream out=new ZipOutputStream(new FileOutputStream(zipfile));
            for(int i=0;i<srcfile.length;i++){
                FileInputStream in=new FileInputStream(srcfile[i]);
                out.putNextEntry(new ZipEntry(srcfile[i].getName()));
                int len;
                while((len=in.read(buf))>0){
                    out.write(buf,0,len);
                }
                out.closeEntry();
                in.close();
            }
            out.close();
            System.out.println("压缩完成.");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 功能:解压缩
     * @param zipfile：需要解压缩的文件
     * @param descDir：解压后的目标目录
     */
    public static void unZipFiles(File zipfile,String descDir){
        try {
            ZipFile zf=new ZipFile(zipfile);
            for(Enumeration entries = zf.entries(); entries.hasMoreElements();){
                ZipEntry entry=(ZipEntry) entries.nextElement();
                String zipEntryName=entry.getName();
                InputStream in=zf.getInputStream(entry);
                OutputStream out=new FileOutputStream(descDir+zipEntryName);
                byte[] buf1=new byte[1024];
                int len;
                while((len=in.read(buf1))>0){
                    out.write(buf1,0,len);
                }
                in.close();
                out.close();
                System.out.println("解压缩完成.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        //2个源文件
        File f1=new File("D:\\workspace\\flexTest\\src\\com\\biao\\test\\abc.txt");
        File f2=new File("D:\\workspace\\flexTest\\src\\com\\biao\\test\\test.zip");
        File[] srcfile={f1,f2};
        //压缩后的文件
        File zipfile=new File("D:\\workspace\\flexTest\\src\\com\\biao\\test\\biao.zip");
        //TestZIP.zipFiles(srcfile, zipfile);
        //需要解压缩的文件
        File file=new File("D:\\workspace\\flexTest\\src\\com\\biao\\test\\biao.zip");
        //解压后的目标目录
        String dir="D:\\workspace\\flexTest\\src\\com\\biao\\test\\";
        //ZipUtils.unZipFiles(file, dir);
    }

}
